package x;

/*
 * DANIEL WHEELER GROUP LEADER
 */

// this will include all events
import java.awt.event.*;
import java.awt.image.BufferStrategy;
import java.awt.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.util.*;
import java.net.*;


public class Game extends Canvas implements KeyListener {


        public static int localPlayerID;
        public static int totalPlayers = 0;

        public long tid = Thread.currentThread().getId();

        public Sound background_song;

        public Image frontIdle;
        public Image frontLeft;
        public Image frontRight;
        public Image upIdle;
        public Image upLeft;
        public Image upRight;
        public Image leftIdle;
        public Image leftLeft;
        public Image leftRight;
        public Image rightIdle;
        public Image rightLeft;
        public Image rightRight;


        /**buildings**/
        public Image saloon;
        public Image new_build;
        public Entity newbuilding;
        public Image cowboy_store;
        public Image shortys_place;
        public Entity shortysplace;
        public Entity saloonbuilding;
        public Entity cowboybuilding;

        /**accessories**/
        public Entity Cactus;
        public Entity Cactus2;
        public Entity Cactus3;
        public Entity Cactus4;

        public Image plant1;
        public Image plant2;
        public Image plant3;
        public Image plant4;


        public BufferStrategy strategy;

        // keyboard movement
        public boolean left;
        public boolean right;
        public boolean up;

        public boolean down;

        public TCPClient client;
        public static Map map;
        public static ArrayList<Entity> players = new ArrayList<Entity>();





        public static Image[] spriteFront=new Image[3];
        public static Image[] spriteUp = new Image[3];
        public static Image[] spriteLeft = new Image[3];
        public static Image[] spriteRight = new Image[3];

        long time = System.currentTimeMillis();
        private Image mback1;


        private Map exitMap;
        private Map2 map2;
        private Map3 map3;
        private Map4 map4;
        private Map5 map5;
        private Entity MapMain;





        /* Below is the URL we will be using for playing sound within the game
         * http://d.fank.ru/d/m5/166735/Sharam_And_Kid_Cudi-She_Came_Along.mp3
         */


        // are game loop so far
        public Game() {
                //loading are sprites within looop, we call are images in here, it allows us to stay organized by stategically placing
                //all sprites and images used within the game in the same location. This makes it alot easier when everyone is working
                //from a different location - DCW


for(int i=0;i<10;i++)
           {
            players.add(Cactus);
       }


                try {
                        URL fI = Thread.currentThread().getContextClassLoader().getResource("x/frontIdle.gif");

                        URL fL= Thread.currentThread().getContextClassLoader().getResource("x/frontLeft.gif");

                        URL fR= Thread.currentThread().getContextClassLoader().getResource("x/frontRight.gif");

                        URL uI = Thread.currentThread().getContextClassLoader().getResource("x/upIdle.gif");

                        URL uL= Thread.currentThread().getContextClassLoader().getResource("x/upLeft.gif");

                        URL uR= Thread.currentThread().getContextClassLoader().getResource("x/upRight.gif");
                        URL lI = Thread.currentThread().getContextClassLoader().getResource("x/leftIdle.gif");

                        URL lL= Thread.currentThread().getContextClassLoader().getResource("x/leftLeft.gif");

                        URL lR= Thread.currentThread().getContextClassLoader().getResource("x/leftRight.gif");
                        URL rI = Thread.currentThread().getContextClassLoader().getResource("x/rightIdle.gif");

                        URL rL= Thread.currentThread().getContextClassLoader().getResource("x/rightLeft.gif");

                        URL rR= Thread.currentThread().getContextClassLoader().getResource("x/rightRight.gif");

                        //buildings

                        URL bone = Thread.currentThread().getContextClassLoader().getResource("x/saloon.gif");

                        URL btwo= Thread.currentThread().getContextClassLoader().getResource("x/cowboystore.gif");

                        URL mapbackground1= Thread.currentThread().getContextClassLoader().getResource("x/MainMap.jpg");

                        URL shorty= Thread.currentThread().getContextClassLoader().getResource("x/shortyplace.gif");

                        URL newImage= Thread.currentThread().getContextClassLoader().getResource("x/bank.gif");


                        //accessories
                        URL cac= Thread.currentThread().getContextClassLoader().getResource("x/cactus.gif");
                        URL cac2= Thread.currentThread().getContextClassLoader().getResource("x/cactus.gif");
                        URL cac3= Thread.currentThread().getContextClassLoader().getResource("x/cactus.gif");
                        URL cac4= Thread.currentThread().getContextClassLoader().getResource("x/cactus.gif");
                        // check for null value of images within the same file as our groups source code

                        mback1 = ImageIO.read(mapbackground1);
                        new_build= ImageIO.read(newImage);
                        shortys_place= ImageIO.read(shorty);
                        frontIdle = ImageIO.read(fI);
                        frontLeft= ImageIO.read(fL);
                        frontRight = ImageIO.read(fR);
                        upIdle = ImageIO.read(uI);
                        upLeft = ImageIO.read(uL);
                        upRight = ImageIO.read(uR);
                        leftIdle = ImageIO.read(lI);
                        leftLeft = ImageIO.read(lL);
                        leftRight = ImageIO.read(lR);
                        rightIdle = ImageIO.read(rI);
                        rightLeft = ImageIO.read(rL);
                        rightRight = ImageIO.read(rR);
                        //** buildings**//
                        saloon= ImageIO.read(bone);
                        cowboy_store= ImageIO.read(btwo);
                        plant1= ImageIO.read(cac);
                        plant2= ImageIO.read(cac2);
                        plant3= ImageIO.read(cac3);
                        plant4= ImageIO.read(cac4);



                        //position elements
                        spriteFront[0]= frontIdle;
                        spriteFront[1]= frontLeft;
                        spriteFront[2]= frontRight;

                        spriteUp[0]= upIdle;
                        spriteUp[1]= upLeft;
                        spriteUp[2]= upRight;

                        spriteLeft[0]= leftIdle;
                        spriteLeft[1]= leftLeft;
                        spriteLeft[2]= leftRight;


                        spriteRight[0]= rightIdle;
                        spriteRight[1]= rightLeft;
                        spriteRight[2]= rightRight;








                } catch (IOException e) {
                        System.err.println("Unable to find graphics");
                        System.exit(0);
                }








                // implementing frame.
                Frame frame = new Frame("Frontier Town ");
                frame.setLayout(null);
                setBounds(5,5,843,640);
                frame.add(this);
                frame.setSize(843,640);
                frame.setResizable(false);

                frame.setLocationRelativeTo(null);


                //----------------------------------------------



                //----------------------------------------------


                // exit game source
                frame.addWindowListener(new WindowAdapter() {

                        public void windowClosing(WindowEvent e) {
                                System.exit(0);
                                // need to exit the current window to get back to the main game
                                
                        }
                });

                // this will listen for keys
                frame.addKeyListener(this);
                addKeyListener(this);

                frame.setVisible(true);

                createBufferStrategy(2);
                strategy = getBufferStrategy();

                // map
                map = new Map();
                map2= new Map2();
                map3= new Map3();
                map4= new Map4();
                exitMap= new Map();
                map5= new Map5();
                //players.inser(new Entity(spriteFront[0], map, 30, 15));
                client = new TCPClient(35000, "localhost");
                        try { Thread.sleep(4000); } catch (Exception e) {};
                System.out.print("Local playerd id is: " + Game.localPlayerID + '\n');
                players.set(Game.localPlayerID,new Entity(spriteFront[0], map, 30, 15));



                //MapMain= new Entity(mback1,map,2,2);

                //**buildings**/
                saloonbuilding = new Entity(saloon, map,40,7 );
                cowboybuilding = new Entity(cowboy_store, map,8 ,7);
                shortysplace= new Entity(shortys_place,map,8,29);
                newbuilding= new Entity(new_build,map,40,29);
                //*accessories
                Cactus=new Entity(plant1, map, 1,1);
                Cactus2=new Entity(plant2,map, 64, 1);
                Cactus3=new Entity(plant3,map, 64, 47);
                Cactus4=new Entity(plant4,map, 1, 47);
                MapMain=new Entity(mback1,map,1,1);

                //if you want to test with network right now uncomment the
                //the tcpclient line and and run the java server program on your
                //local machine, we can change this once we figure out what
                //server to put this on.
                //server to put the java program on.
                //TCPClient client = new TCPClient(35000, "localhost");
                // start the game loop for now
                gameLoop();
        }

        //timing and our collison so far
        public void gameLoop() {

                
                long last = System.nanoTime();
                int counter = 0;
                while (true) {

                        background_song= new Sound("src/x/minigame1/");

                        background_song.loop(); // play if try is a success...
                   
                            

                        //above looping through the song while the game loop remains true

                        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
                        //g.setColor(Color.white);
                        //g.fillRect(0,0,843,678);

                        // rendering our game objects
                        g.translate(100,100);
                      

                          /*building painting*/
                        /*

                         */
                        if(Game.map == map)
                        {
                            super.paint(g);

                            map.paint(g);

                        cowboybuilding.paint(g);
                        saloonbuilding.paint(g);
                        shortysplace.paint(g);
                        newbuilding.paint(g);

                        }
                        if(Game.map == map2)
                        {

                            super.paint(g);
                            map2.paint(g);
                        }
                        if(Game.map == map3)
                        {
                            super.paint(g);
                            map3.paint(g);
                        }
                        if(Game.map == map4)
                        {
                            super.paint(g);
                            map4.paint(g);
                        }
                        if(Game.map == map5)
                        {
                            super.paint(g);
                            map5.paint(g);
                        }
                        //player movements!!!!!!!!
                       // System.out.print(totalPlayers +" is the number of players \n");
                       // System.out.print(players.size() + "is the actual size of vector \n");

                        for(int i = 0; i<totalPlayers;i++)
                        {
                            if(players.get(i).moveDown)
                                players.get(i).image = spriteFront[counter%3];
                            if(players.get(i).moveUp)
                                players.get(i).image = spriteUp[counter%3];
                            if(players.get(i).moveLeft)
                                players.get(i).image = spriteLeft[counter%3];
                            if(players.get(i).moveRight)
                                players.get(i).image = spriteRight[counter%3];
                            if(players.get(i).area==players.get(localPlayerID).area)
                            {
                                players.get(i).paint(g);
                            }
                        }


                        // how does this look
                        // lets implement the map features for moving from map to map
                        if(map.location == 3)
                        {

                            Game.map = map5;
                            Game.players.set(localPlayerID, new Entity(spriteFront[0],map,32,45,4));
                            String packet ="5 "+Game.localPlayerID+" "+4+ " ";
                            client.cw.run(packet);


                        }

                        if(map.location == 2)
                        {
                            Game.map = map3;
                            Game.players.set(localPlayerID, new Entity(spriteFront[0],map,32,45,2));
                            String packet ="5 "+Game.localPlayerID+" "+2+ " ";
                            client.cw.run(packet);
                        }


                        if(map.location  == 1)
                        {
                            Game.map= map2;
                            Game.players.set(localPlayerID, new Entity(spriteFront[0],map,32,45,1));
                            String packet ="5 "+Game.localPlayerID+" "+1+ " ";
                            client.cw.run(packet);
                        }

                        if(map.location  == 4)
                        {
                            Game.map= map4;
                            Game.players.set(localPlayerID, new Entity(spriteFront[0],map,32,45,3));
                            String packet ="5 "+Game.localPlayerID+" "+3+ " ";
                            client.cw.run(packet);
                        }

                        // going back to the main map  from the minigames
                        //*******************************************************************************
                        if(players.get(localPlayerID).location == 12) // map2
                        {

                            Game.map= exitMap;
                            Game.players.set(localPlayerID, new Entity(spriteFront[0],map,17,22,0));
                            String packet ="5 "+Game.localPlayerID+" "+0+ " ";
                            client.cw.run(packet);

                        }

                        if(players.get(localPlayerID).location == 13) // map3
                        {
                            Game.map= exitMap;
                            Game.players.set(localPlayerID, new Entity(spriteFront[0],map,48,22,0));
                            String packet ="5 "+Game.localPlayerID+" "+0+ " ";
                            client.cw.run(packet);


                        }


                        // ------------bottom buildings-------------------


                        if(players.get(localPlayerID).location == 14) // map4
                        {
                            Game.map= exitMap;
                            Game.players.set(localPlayerID,new Entity(spriteFront[0],map,48,45,0));
                            String packet ="5 "+Game.localPlayerID+" "+0 + " ";
                            client.cw.run(packet);

                        }

                        if(players.get(localPlayerID).location == 15) // map5
                        {
                            Game.map= exitMap;
                            Game.players.set(localPlayerID, new Entity(spriteFront[0],map,17,45,0));
                            String packet ="5 "+Game.localPlayerID+" "+0 + " ";
                            client.cw.run(packet);

                        }


                        // LAUCHING THE MINI GAMES
                        if(players.get(localPlayerID).location == 69) // map5
                        {
                           //new SplashScreen2(5000);
                           new Minigame1(); // this will open the mini game, however I need to control the exit status
                           

                        }


                        


                        g.dispose();
                        strategy.show();

                        // system pausing
                        try { Thread.sleep(4); } catch (Exception e) {};

                        long delta = (System.nanoTime() - last) / 1000000;
                        last = System.nanoTime();

                        //update time
                        for (int i=0;i<delta / 5;i++) {
                                logic(5);
                        }
                        //update again if needed
                        if ((delta % 5) != 0) {
                                logic(delta % 5);
                        }
                        counter++;
                }
        }

        public void logic(long delta) {
                // check the keyboard and record which way the player
                // is trying to move this loop

                float dx = 0;
                float dy = 0;
                if (left) {
                        dx -= 1;
                }
                if (right) {
                        dx += 1;
                }
                if (up) {
                        dy -= 1;
                }
                if (down) {
                        dy += 1;
                }

                // if the player needs to move attempt to move the entity
                // based on the keys multiplied by the amount of time thats
                // passed
                if ((dx != 0) || (dy != 0)) {

                            if(players.get(localPlayerID).move(dx * delta * 0.003f, dy * delta * 0.003f))
                            {

                                String packet ="1 "+Game.localPlayerID+" "+players.get(Game.localPlayerID).Name+" "+players.get(Game.localPlayerID).x+" "+players.get(Game.localPlayerID).y + " ";
                                client.cw.run(packet);
                            }
                }
        }

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
                // check the keyboard and record which keys are pressed
                if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                        left = true;
                }
                if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                        right = true;
                }
                if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                        down = true;
                }
                if (e.getKeyCode() == KeyEvent.VK_UP) {
                        up  = true;
                }
        }

 public void keyReleased(KeyEvent e) {
                // check the keyboard and record which keys are released
                if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                        players.get(Game.localPlayerID).moveDown = false;
                        players.get(Game.localPlayerID).moveUp = false;
                        players.get(Game.localPlayerID).moveRight = false;
                        players.get(Game.localPlayerID).moveLeft = false;
                        left = false;
                }
                if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                        players.get(Game.localPlayerID).moveDown = false;
                        players.get(Game.localPlayerID).moveUp = false;
                        players.get(Game.localPlayerID).moveRight = false;
                        players.get(Game.localPlayerID).moveLeft = false;
                        right = false;
                }
                if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                        players.get(Game.localPlayerID).moveDown = false;
                        players.get(Game.localPlayerID).moveUp = false;
                        players.get(Game.localPlayerID).moveRight = false;
                        players.get(Game.localPlayerID).moveLeft = false;
                        down = false;
                }
                if (e.getKeyCode() == KeyEvent.VK_UP) {
                        players.get(Game.localPlayerID).moveDown = false;
                        players.get(Game.localPlayerID).moveUp = false;
                        players.get(Game.localPlayerID).moveRight = false;
                        players.get(Game.localPlayerID).moveLeft = false;
                        up = false;
                }
        }









        public static void main(String[] argv) {
                //new SplashScreen();
                //new SplashScreen(10000);

                new SplashScreen(5000);
                new Game();
        }





}
